﻿// ***********************************************************************
// Project			: Micua
// Assembly         : Micua.Core
// Author           : iceStone
// Created          : 2013-11-23 22:40
//
// Last Modified By : iceStone
// Last Modified On : 2013-11-23 22:40
// ***********************************************************************
// <copyright file="BundleConfig.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary>缺少注释</summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Xml;
using Micua.Utility;

namespace Micua.UI.Config
{
    /// <summary>
    /// 资源文件捆绑配置
    /// </summary>
    /// <remarks>
    ///  2013-11-23 22:44 Created By iceStone
    /// </remarks>
    public class BundleConfig
    {
        /// <summary>
        /// The bundle configuration path
        /// </summary>
        const string BundleConfigPath = "~/Config/Bundle.config";

        /// <summary>
        /// 资源文件捆绑
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:44 Created By iceStone
        /// </remarks>
        /// <exception cref="System.Exception">
        /// 未配置/Config/Bundle.config文件
        /// </exception>
        public static void RegisterBundles(BundleCollection bundles)
        {
            var doc = new XmlDocument();
            var settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略Xml中的注释  
            settings.IgnoreWhitespace = true;//忽略没必要的空白  
            using (var reader = XmlReader.Create(ApplicationHelper.MapPath(BundleConfigPath), settings))
            {
                doc.Load(reader);
            }

            if (doc == null) { throw new Exception("未配置/Config/Bundle.config文件"); }

            XmlNode root = doc.DocumentElement;
            if (root == null) { throw new Exception("未配置/Config/Bundle.config文件"); }

            // Regester Script 
            var scriptList = root.SelectNodes("scripts/script");
            if (scriptList != null && scriptList.Count > 0)
            {
                foreach (XmlNode node in scriptList)
                {
                    string path = CheckNodeRegedit(node);
                    if (string.IsNullOrEmpty(path)) continue;
                    var bound = new ScriptBundle(path);
                    var files = GetFilesFormNode(node);
                    if (files.Count <= 0) continue;
                    bound.Include(files.ToArray());
                    bundles.Add(bound);
                }
            }
            // Regester Style 
            var styleList = root.SelectNodes("styles/style");
            if (styleList != null && styleList.Count > 0)
            {
                foreach (XmlNode node in styleList)
                {
                    string path = CheckNodeRegedit(node);
                    if (string.IsNullOrEmpty(path)) continue;
                    var bound = new StyleBundle(path);
                    var files = GetFilesFormNode(node);
                    if (files.Count <= 0) continue;
                    bound.Include(files.ToArray());
                    bundles.Add(bound);
                }
            }

            //BundleTable.EnableOptimizations = true;
        }
        /// <summary>
        /// 如果内容为空则不添加
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:44 Created By iceStone
        /// </remarks>
        /// <param name="node">The node.</param>
        /// <returns>List{System.String}.</returns>
        private static List<string> GetFilesFormNode(XmlNode node)
        {
            return (from XmlNode nodeFile in node.ChildNodes
                    where !string.IsNullOrEmpty(nodeFile.InnerText.Trim())
                    select nodeFile.InnerText.Trim()).ToList();
        }

        /// <summary>
        /// 检查注册的Node
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:44 Created By iceStone
        /// </remarks>
        /// <param name="node">The node.</param>
        /// <returns>System.String.</returns>
        private static string CheckNodeRegedit(XmlNode node)
        {
            if (node.Attributes == null) return string.Empty;
            var pathAtt = node.Attributes["path"];
            if (pathAtt == null || string.IsNullOrEmpty(pathAtt.Value.Trim()) || node.ChildNodes.Count == 0)
                return string.Empty;
            return pathAtt.Value.Trim();
        }


        ///// <summary>
        ///// 注册捆绑配置
        ///// </summary>
        //public static void RegisterBundles(BundleCollection bundles)
        //{
        //    ////后台基本样式
        //    //var adminCssBase = ;
        //    //bundles.Add(new Bundle("~/content/scripts/base").Include("~/Content/Scripts/bootstrap/bootstrap.js"));
        //    bundles.Add(new ScriptBundle("~/content/scripts/base").Include(
        //        "~/Content/Scripts/jquery/jquery.js"
        //        , "~/Content/Scripts/bootstrap/bootstrap.js"
        //        , "~/Content/Scripts/admin.js"
        //    ));
        //    bundles.Add(new StyleBundle("~/content/styles/base").Include(
        //        "~/Content/Styles/admin-layout.css"
        //        , "~/Content/Styles/bootstrap.css"
        //        , "~/Content/Styles/bootstrap-responsive.css"
        //    ));
        //    BundleTable.EnableOptimizations = true;
        //}
    }


}